package com.ymatou.apigateway.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.apigateway.parameter.AesUtil;
import com.ymatou.apigateway.parameter.OpenapiReq;
import com.ymatou.apigateway.parameter.SkuPrice;
import com.ymatou.apigateway.parameter.V1updateproductpriceBean;
import com.ymatou.apigateway.service.OpenapiService;
import com.ymatou.apigateway.service.V1updateproductpriceCall;
import com.ymatou.iapi.sellerproduct.parameter.SKUPriceBean;
import com.ymatou.iapi.sellerproduct.service.AddNewSellerProductCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.Application;
import com.ymttest.database.model.AuthCode;
import com.ymttest.database.mongo.ProductDetailModelWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.mongo.jproduct.CatalogsWapper;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*更新商品价格
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_V1UpdateProductPrice {

	private static OpenapiReq openapiReq;
	private static V1updateproductpriceBean V1updateproductpriceBean;
	private static V1updateproductpriceCall v1updateproductpriceCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新商品价格");
	}

	@Before
	public void caseUp() {
		openapiReq = new OpenapiReq();
		openapiReq.setMethod("ymatou.sku.price.update");

		V1updateproductpriceBean = new V1updateproductpriceBean();
		v1updateproductpriceCall = new V1updateproductpriceCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(v1updateproductpriceCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_V1UpdateProductPrice_001() {
		Logger.start(true, "根据sku_id更新商品价格");
		try {
			Integer sellerId = Integer.valueOf(EnvSetup
					.getData("productsellerid"));
			String sellerName = EnvSetup.getData("productsellername");

			JSONObject addProductBean = JsonBeanHelper
					.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc10.json");
			JsonBeanHelper.updateJsonBean(addProductBean,
					MapUtil.hashMap("UserID", sellerId, "LoginID", sellerName));
			List<String> skuList = generateSKUList(8);
			setSkuForAddProduct(skuList, addProductBean);

			//发布商品 带SKU
			AddNewSellerProductCall addNewSellerProductCall = new AddNewSellerProductCall();

			String productId = addNewSellerProductCall
					.callAndGetReturnData(addProductBean).getJSONObject("Data")
					.getString("ProductID");
			Logger.comment("ProductId: " + productId);

			//===============================================================================================================
			List<SKUPriceBean> skuPriceList1 = generateSKUStock(skuList, true,
					true, true);
			//获取修改前价格
			List<SKUPriceBean> originalPriceList1 = getOriginalSKUPriceList(skuList);

			List<SkuPrice> skuPrices = new ArrayList<SkuPrice>();

			for (SKUPriceBean skuPrice : skuPriceList1) {
				SkuPrice price = new SkuPrice();
				price.setOuter_sku_id(skuPrice.getSkuId());
				price.setProduct_price(new BigDecimal(skuPrice.getPrice())
						.setScale(2, BigDecimal.ROUND_HALF_UP));
				price.setProduct_vip_price(new BigDecimal(skuPrice
						.getVipPrice()).setScale(2, BigDecimal.ROUND_HALF_UP));
				price.setProduct_newuser_price(new BigDecimal(skuPrice
						.getNewPrice()).setScale(2, BigDecimal.ROUND_HALF_UP));

				skuPrices.add(price);
			}

			V1updateproductpriceBean.setSku_prices(skuPrices);

			OpenApiWapper openApiWapper = new OpenApiWapper();
			EnvSetup.getData("SellerId");
			Application application = openApiWapper
					.selectApplicationBySellerId(
							Long.valueOf(EnvSetup.getData("productsellerid")))
					.get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(V1updateproductpriceBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.sku.price.update",
							app_id);
			v1updateproductpriceCall.setUrl(url);

			v1updateproductpriceCall.setData(openapiReq);
			v1updateproductpriceCall.callService();

			JSONObject retBean1 = new JSONObject(
					v1updateproductpriceCall.getReturnData());
			Logger.verifyEquals("0000", retBean1.getString("code"), "返回code检查");
			//check
			checkPriceBySku(
					retBean1.getJSONObject("content").getJSONArray("results"),
					skuPriceList1, originalPriceList1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_V1UpdateProductPrice_002() {
		Logger.start(true, "更新商品价格-商品原价空");
		try {
			List<SkuPrice> skuPrices = new ArrayList<SkuPrice>();

			SkuPrice price = new SkuPrice();
			price.setOuter_sku_id("SKU73846146");
			price.setProduct_vip_price(new BigDecimal(4.00).setScale(2,
					BigDecimal.ROUND_HALF_UP));
			price.setProduct_newuser_price(new BigDecimal(3.23).setScale(2,
					BigDecimal.ROUND_HALF_UP));

			skuPrices.add(price);

			V1updateproductpriceBean.setSku_prices(skuPrices);

			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId(
							Long.valueOf(EnvSetup.getData("SellerId"))).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(V1updateproductpriceBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.sku.price.update",
							app_id);
			v1updateproductpriceCall.setUrl(url);

			v1updateproductpriceCall.setData(openapiReq);
			v1updateproductpriceCall.callService();

			Logger.verifyEquals("0001", v1updateproductpriceCall.getCode(),
					"返回code检查");
			//			Logger.verifyEquals("false", v1updateproductpriceCall.getSuccess(), "返回success检查");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_V1UpdateProductPrice_003() {
		Logger.start(true, "更新商品价格-SkuId空");
		try {
			List<SkuPrice> skuPrices = new ArrayList<SkuPrice>();

			SkuPrice price = new SkuPrice();
			price.setProduct_price(new BigDecimal(4.56).setScale(2,
					BigDecimal.ROUND_HALF_UP));
			price.setProduct_vip_price(new BigDecimal(4.00).setScale(2,
					BigDecimal.ROUND_HALF_UP));
			price.setProduct_newuser_price(new BigDecimal(3.23).setScale(2,
					BigDecimal.ROUND_HALF_UP));

			skuPrices.add(price);

			V1updateproductpriceBean.setSku_prices(skuPrices);

			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId(
							Long.valueOf(EnvSetup.getData("SellerId"))).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(V1updateproductpriceBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.sku.price.update",
							app_id);
			v1updateproductpriceCall.setUrl(url);

			v1updateproductpriceCall.setData(openapiReq);
			v1updateproductpriceCall.callService();

			Logger.verifyEquals("0001", v1updateproductpriceCall.getCode(),
					"返回code检查");
			Logger.verifyEquals("outer_sku_id 或 sku_id 必传一个.",
					v1updateproductpriceCall.getMessage(), "返回success检查");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_V1UpdateProductPrice_004() {
		Logger.start(true, "更新商品价格-商品原价负数");
		try {
			List<SkuPrice> skuPrices = new ArrayList<SkuPrice>();

			SkuPrice price = new SkuPrice();
			price.setOuter_sku_id("SKU73846146");
			price.setProduct_price(new BigDecimal(-4.56).setScale(2,
					BigDecimal.ROUND_HALF_UP));
			price.setProduct_vip_price(new BigDecimal(4.00).setScale(2,
					BigDecimal.ROUND_HALF_UP));
			price.setProduct_newuser_price(new BigDecimal(3.23).setScale(2,
					BigDecimal.ROUND_HALF_UP));

			skuPrices.add(price);

			V1updateproductpriceBean.setSku_prices(skuPrices);

			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId(
							Long.valueOf(EnvSetup.getData("SellerId"))).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(V1updateproductpriceBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.sku.price.update",
							app_id);
			v1updateproductpriceCall.setUrl(url);

			v1updateproductpriceCall.setData(openapiReq);
			v1updateproductpriceCall.callService();

			Logger.verifyEquals("0001", v1updateproductpriceCall.getCode(),
					"返回code检查");
			//			Logger.verifyEquals("false", v1updateproductpriceCall.getSuccess(), "返回success检查");	

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_V1UpdateProductPrice_005() {
		Logger.start(true, "更新商品价格-商品VIP价负数");
		try {
			List<SkuPrice> skuPrices = new ArrayList<SkuPrice>();

			SkuPrice price = new SkuPrice();
			price.setOuter_sku_id("SKU73846146");
			price.setProduct_price(new BigDecimal(4.56).setScale(2,
					BigDecimal.ROUND_HALF_UP));
			price.setProduct_vip_price(new BigDecimal(-4.00).setScale(2,
					BigDecimal.ROUND_HALF_UP));
			price.setProduct_newuser_price(new BigDecimal(3.23).setScale(2,
					BigDecimal.ROUND_HALF_UP));

			skuPrices.add(price);

			V1updateproductpriceBean.setSku_prices(skuPrices);

			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId(
							Long.valueOf(EnvSetup.getData("SellerId"))).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(V1updateproductpriceBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.sku.price.update",
							app_id);
			v1updateproductpriceCall.setUrl(url);

			v1updateproductpriceCall.setData(openapiReq);
			v1updateproductpriceCall.callService();

			Logger.verifyEquals("0001", v1updateproductpriceCall.getCode(),
					"返回code检查");
			//			Logger.verifyEquals("false", v1updateproductpriceCall.getSuccess(), "返回success检查");		

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_V1UpdateProductPrice_006() {
		Logger.start(true, "更新商品价格-商品新客价负数");
		try {
			List<SkuPrice> skuPrices = new ArrayList<SkuPrice>();

			SkuPrice price = new SkuPrice();
			price.setOuter_sku_id("SKU73846146");
			price.setProduct_price(new BigDecimal(4.56).setScale(2,
					BigDecimal.ROUND_HALF_UP));
			price.setProduct_vip_price(new BigDecimal(4.00).setScale(2,
					BigDecimal.ROUND_HALF_UP));
			price.setProduct_newuser_price(new BigDecimal(-3.23).setScale(2,
					BigDecimal.ROUND_HALF_UP));

			skuPrices.add(price);

			V1updateproductpriceBean.setSku_prices(skuPrices);

			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId(
							Long.valueOf(EnvSetup.getData("SellerId"))).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(V1updateproductpriceBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.sku.price.update",
							app_id);
			v1updateproductpriceCall.setUrl(url);

			v1updateproductpriceCall.setData(openapiReq);
			v1updateproductpriceCall.callService();

			Logger.verifyEquals("0001", v1updateproductpriceCall.getCode(),
					"返回code检查");
			//			Logger.verifyEquals("false", v1updateproductpriceCall.getSuccess(), "返回success检查");	

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_V1UpdateProductPrice_007() {
		Logger.start(true, "更新商品价格-商品原价小数点后4位");
		try {
			List<SkuPrice> skuPrices = new ArrayList<SkuPrice>();

			SkuPrice price = new SkuPrice();
			price.setOuter_sku_id("SKU73846146");
			price.setProduct_price(new BigDecimal(4.56).setScale(4,
					BigDecimal.ROUND_HALF_UP));
			price.setProduct_vip_price(new BigDecimal(4.00).setScale(2,
					BigDecimal.ROUND_HALF_UP));
			price.setProduct_newuser_price(new BigDecimal(3.23).setScale(2,
					BigDecimal.ROUND_HALF_UP));

			skuPrices.add(price);

			V1updateproductpriceBean.setSku_prices(skuPrices);

			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId(
							Long.valueOf(EnvSetup.getData("SellerId"))).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(V1updateproductpriceBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.sku.price.update",
							app_id);
			v1updateproductpriceCall.setUrl(url);

			v1updateproductpriceCall.setData(openapiReq);
			v1updateproductpriceCall.callService();

			Logger.verifyEquals("0001", v1updateproductpriceCall.getCode(),
					"返回code检查");
			//			Logger.verifyEquals("false", v1updateproductpriceCall.getSuccess(), "返回success检查");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_V1UpdateProductPrice_008() {
		Logger.start(true, "更新商品价格-商品VIP价小数点后4位");
		try {
			List<SkuPrice> skuPrices = new ArrayList<SkuPrice>();

			SkuPrice price = new SkuPrice();
			price.setOuter_sku_id("SKU73846146");
			price.setProduct_price(new BigDecimal(4.56).setScale(2,
					BigDecimal.ROUND_HALF_UP));
			price.setProduct_vip_price(new BigDecimal(4.00).setScale(4,
					BigDecimal.ROUND_HALF_UP));
			price.setProduct_newuser_price(new BigDecimal(3.23).setScale(2,
					BigDecimal.ROUND_HALF_UP));

			skuPrices.add(price);

			V1updateproductpriceBean.setSku_prices(skuPrices);

			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId(
							Long.valueOf(EnvSetup.getData("SellerId"))).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(V1updateproductpriceBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.sku.price.update",
							app_id);
			v1updateproductpriceCall.setUrl(url);

			v1updateproductpriceCall.setData(openapiReq);
			v1updateproductpriceCall.callService();

			Logger.verifyEquals("0001", v1updateproductpriceCall.getCode(),
					"返回code检查");
			//			Logger.verifyEquals("false", v1updateproductpriceCall.getSuccess(), "返回success检查");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_V1UpdateProductPrice_009() {
		Logger.start(true, "更新商品价格-商品新客价小数点后4位");
		try {
			List<SkuPrice> skuPrices = new ArrayList<SkuPrice>();

			SkuPrice price = new SkuPrice();
			price.setOuter_sku_id("SKU73846146");
			price.setProduct_price(new BigDecimal(4.56).setScale(2,
					BigDecimal.ROUND_HALF_UP));
			price.setProduct_vip_price(new BigDecimal(4.00).setScale(2,
					BigDecimal.ROUND_HALF_UP));
			price.setProduct_newuser_price(new BigDecimal(3.23).setScale(4,
					BigDecimal.ROUND_HALF_UP));

			skuPrices.add(price);

			V1updateproductpriceBean.setSku_prices(skuPrices);

			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId(
							Long.valueOf(EnvSetup.getData("SellerId"))).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(V1updateproductpriceBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.sku.price.update",
							app_id);
			v1updateproductpriceCall.setUrl(url);

			v1updateproductpriceCall.setData(openapiReq);
			v1updateproductpriceCall.callService();

			Logger.verifyEquals("0001", v1updateproductpriceCall.getCode(),
					"返回code检查");
			//			Logger.verifyEquals("false", v1updateproductpriceCall.getSuccess(), "返回success检查");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_V1UpdateProductPrice_010() {
		Logger.start(true, "更新他人商品价格");
		try {
			Integer sellerId = Integer.valueOf(EnvSetup
					.getData("productsellerid"));
			String sellerName = EnvSetup.getData("productsellername");

			JSONObject addProductBean = JsonBeanHelper
					.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc10.json");
			JsonBeanHelper.updateJsonBean(addProductBean,
					MapUtil.hashMap("UserID", sellerId, "LoginID", sellerName));
			List<String> skuList = generateSKUList(8);
			setSkuForAddProduct(skuList, addProductBean);

			//发布商品 带SKU
			AddNewSellerProductCall addNewSellerProductCall = new AddNewSellerProductCall();

			String productId = addNewSellerProductCall
					.callAndGetReturnData(addProductBean).getJSONObject("Data")
					.getString("ProductID");
			Logger.comment("ProductId: " + productId);

			//===============================================================================================================
			List<SKUPriceBean> skuPriceList1 = generateSKUStock(skuList, true,
					true, true);
			//获取修改前价格
			List<SKUPriceBean> originalPriceList1 = getOriginalSKUPriceList(skuList);

			List<SkuPrice> skuPrices = new ArrayList<SkuPrice>();

			for (SKUPriceBean skuPrice : skuPriceList1) {
				SkuPrice price = new SkuPrice();
				price.setOuter_sku_id(skuPrice.getSkuId());
				price.setProduct_price(new BigDecimal(skuPrice.getPrice())
						.setScale(2, BigDecimal.ROUND_HALF_UP));
				price.setProduct_vip_price(new BigDecimal(skuPrice
						.getVipPrice()).setScale(2, BigDecimal.ROUND_HALF_UP));
				price.setProduct_newuser_price(new BigDecimal(skuPrice
						.getNewPrice()).setScale(2, BigDecimal.ROUND_HALF_UP));

				skuPrices.add(price);
			}

			V1updateproductpriceBean.setSku_prices(skuPrices);

			OpenApiWapper openApiWapper = new OpenApiWapper();
			EnvSetup.getData("SellerId");
			Application application = openApiWapper
					.selectApplicationBySellerId(
							Long.valueOf(EnvSetup.getData("SellerId2"))).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(V1updateproductpriceBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.sku.price.update",
							app_id);
			v1updateproductpriceCall.setUrl(url);

			v1updateproductpriceCall.setData(openapiReq);
			v1updateproductpriceCall.callService();

			JSONObject retBean1 = new JSONObject(
					v1updateproductpriceCall.getReturnData());
			Logger.verifyEquals("0000", retBean1.getString("code"), "返回code检查");

			JSONArray jsonArray = retBean1.getJSONObject("content")
					.getJSONArray("results");

			for (int j = 0; j < jsonArray.length(); j++) {

				Logger.verifyEquals(false, jsonArray.getJSONObject(j)
						.getBoolean("success"), "返回报文检查");
				Logger.verifyEquals("无效的SKUID", jsonArray.getJSONObject(j)
						.getString("msg"), "返回报文msg检查");
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_V1UpdateProductPrice_011() {
		Logger.start(true, "更新商品价格-商品原价小数点后多位");
		try {
			List<SkuPrice> skuPrices = new ArrayList<SkuPrice>();

			SkuPrice price = new SkuPrice();
			price.setOuter_sku_id("SKU73846146");
			price.setProduct_price(new BigDecimal(4.56));
			price.setProduct_vip_price(new BigDecimal(4.00));
			price.setProduct_newuser_price(new BigDecimal(3.23));

			skuPrices.add(price);

			V1updateproductpriceBean.setSku_prices(skuPrices);

			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId(
							Long.valueOf(EnvSetup.getData("SellerId"))).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(V1updateproductpriceBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.sku.price.update",
							app_id);
			v1updateproductpriceCall.setUrl(url);

			v1updateproductpriceCall.setData(openapiReq);
			v1updateproductpriceCall.callService();

			Logger.verifyEquals("0001", v1updateproductpriceCall.getCode(),
					"返回code检查");
			Logger.verifyEquals(
					"skuPrices[0].newPrice:numeric value out of bounds (<18 digits>.<2 digits> expected)|skuPrices[0].price:numeric value out of bounds (<18 digits>.<2 digits> expected)",
					v1updateproductpriceCall.getMessage(), "返回success检查");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_V1UpdateProductPrice_012() {
		Logger.start(true, "根据catalog_id更新商品价格");
		try {
			Integer sellerId = Integer.valueOf(EnvSetup
					.getData("productsellerid"));
			String sellerName = EnvSetup.getData("productsellername");

			JSONObject addProductBean = JsonBeanHelper
					.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc10.json");
			JsonBeanHelper.updateJsonBean(addProductBean,
					MapUtil.hashMap("UserID", sellerId, "LoginID", sellerName));
			List<String> skuList = generateSKUList(8);

			setSkuForAddProduct(skuList, addProductBean);

			//发布商品 带SKU
			AddNewSellerProductCall addNewSellerProductCall = new AddNewSellerProductCall();

			String productId = addNewSellerProductCall
					.callAndGetReturnData(addProductBean).getJSONObject("Data")
					.getString("ProductID");
			Logger.comment("ProductId: " + productId);

			//===============================================================================================================
			List<SKUPriceBean> skuPriceList1 = generateSKUStock(skuList, true,
					true, true);
			//获取修改前价格
			List<SKUPriceBean> originalPriceList1 = getOriginalSKUPriceList(skuList);

			List<SkuPrice> skuPrices = new ArrayList<SkuPrice>();

			for (SKUPriceBean skuPrice : skuPriceList1) {
				SkuPrice price = new SkuPrice();

				String sku_id = skuPrice.getSkuId();

				List<Map> maps = new YmtCatalogsIWapper()
						.selectCatalogsByProductIdandSku(productId, sku_id);
				String catalog_id = (String) maps.get(0).get("sCatalogId");

				price.setSku_id(catalog_id);
				price.setProduct_price(new BigDecimal(skuPrice.getPrice())
						.setScale(2, BigDecimal.ROUND_HALF_UP));
				price.setProduct_vip_price(new BigDecimal(skuPrice
						.getVipPrice()).setScale(2, BigDecimal.ROUND_HALF_UP));
				price.setProduct_newuser_price(new BigDecimal(skuPrice
						.getNewPrice()).setScale(2, BigDecimal.ROUND_HALF_UP));

				skuPrices.add(price);
			}

			V1updateproductpriceBean.setSku_prices(skuPrices);

			OpenApiWapper openApiWapper = new OpenApiWapper();
			EnvSetup.getData("SellerId");
			Application application = openApiWapper
					.selectApplicationBySellerId(
							Long.valueOf(EnvSetup.getData("productsellerid")))
					.get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(V1updateproductpriceBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.sku.price.update",
							app_id);
			v1updateproductpriceCall.setUrl(url);

			v1updateproductpriceCall.setData(openapiReq);
			v1updateproductpriceCall.callService();

			JSONObject retBean1 = new JSONObject(
					v1updateproductpriceCall.getReturnData());
			Logger.verifyEquals("0000", retBean1.getString("code"), "返回code检查");
			//check
			checkPriceBySku(
					retBean1.getJSONObject("content").getJSONArray("results"),
					skuPriceList1, originalPriceList1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	public static List<String> generateSKUList(int num) {
		List<String> skuList = new ArrayList<>();
		for (int i = 0; i < num; i++) {
			skuList.add("SKU" + TestDataManager.getRandomIntNum(10));
		}
		return skuList;
	}

	public static List<SKUPriceBean> generateSKUStock(List<String> skuList,
			boolean isPriceUpdate, boolean isVipPriceUpdate,
			boolean isNewPriceUpdate) {
		List<SKUPriceBean> retList = new ArrayList<>();

		skuList.forEach(ele -> {
			SKUPriceBean sKUPriceBean = new SKUPriceBean();
			sKUPriceBean.setSkuId(ele);
			if (isPriceUpdate) {
				sKUPriceBean.setPrice((double) TestDataManager
						.getRandomIntNum(6) / 100);
			}
			if (isVipPriceUpdate) {
				sKUPriceBean.setVipPrice((double) TestDataManager
						.getRandomIntNum(5) / 100);
			}
			if (isNewPriceUpdate) {
				sKUPriceBean.setNewPrice((double) TestDataManager
						.getRandomIntNum(4) / 100);
			}
			retList.add(sKUPriceBean);
		});
		return retList;
	}

	public static void setSkuForAddProduct(List<String> skuList,
			JSONObject addProdRequest) throws JSONException {
		JSONArray catalogAry = addProdRequest.getJSONObject("Product")
				.getJSONArray("Catalogs");
		int minNum = Math.min(skuList.size(), catalogAry.length());
		for (int i = 0; i < minNum; i++) {
			catalogAry.getJSONObject(i).put("SKU", skuList.get(i));
		}
	}

	@SuppressWarnings({"rawtypes", "unchecked"})
	public static List<SKUPriceBean> getOriginalSKUPriceList(
			List<String> skuList) {
		List<SKUPriceBean> retSKUPriceBeanList = new ArrayList<>();
		YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();

		skuList.forEach(skuId -> {

			try {
				List<Map> retList = ymtCatalogsIWapper
						.selectProductIdBySku(skuId);
				Logger.verifyEquals(1, retList.size(), "SKU:" + skuId
						+ ", 应该只有一个对应的catalog");
				Map<String, Object> ret = retList.get(0);

				double actPriceInSql = Double.parseDouble(ret
						.get("fQuotePrice").toString());
				double actVipPriceInSql = Double.parseDouble(ret.get(
						"fVipPrice").toString());
				double actNewPriceInSql = Double.parseDouble(ret.get(
						"fNewCustomerPrice").toString());

				SKUPriceBean sKUPriceBean = new SKUPriceBean();
				sKUPriceBean.setSkuId(skuId);
				sKUPriceBean.setPrice(actPriceInSql);
				sKUPriceBean.setVipPrice(actVipPriceInSql);
				sKUPriceBean.setNewPrice(actNewPriceInSql);
				retSKUPriceBeanList.add(sKUPriceBean);

			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		});
		return retSKUPriceBeanList;
	}

	/***
	 * 
	 * @param skuResults 返回报文skuResults节点
	 * @param updateSKUPriceList 修改价格bean
	 * @param orginalSKUPriceList 原来的价格bean
	 */
	@SuppressWarnings({"unchecked", "rawtypes"})
	public static void checkPriceBySku(JSONArray skuResults,
			List<SKUPriceBean> updateSKUPriceList,
			List<SKUPriceBean> originalSKUPriceList) {
		YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();

		updateSKUPriceList.forEach(updateSKUPrice -> {
			try {
				SKUPriceBean orginalSKUPrice = originalSKUPriceList
						.stream()
						.filter(ele -> ele.getSkuId().equals(
								updateSKUPrice.getSkuId())).findFirst()
						.orElse(null);

				double tgtPrice = updateSKUPrice.getPrice() == -1
						? orginalSKUPrice.getPrice()
						: updateSKUPrice.getPrice();
				double tgtVipPrice = updateSKUPrice.getVipPrice() == -1
						? orginalSKUPrice.getVipPrice()
						: updateSKUPrice.getVipPrice();
				double tgtNewPrice = updateSKUPrice.getNewPrice() == -1
						? orginalSKUPrice.getNewPrice()
						: updateSKUPrice.getNewPrice();;

				//返回报文检查
				String skuId = updateSKUPrice.getSkuId();

				boolean isFoundInRet = false;
				for (int j = 0; j < skuResults.length(); j++) {
					if (skuResults.getJSONObject(j).getString("outer_sku_id")
							.equals(skuId)) {
						Logger.verifyEquals(true, skuResults.getJSONObject(j)
								.getBoolean("success"), "ExecSuccess 返回报文检查");
						isFoundInRet = true;
						break;
					}
				}
				Logger.verifyEquals(true, isFoundInRet, "SKU:" + skuId
						+ ",在返回报文中是否存在");

				//sql
				List<Map> retList = ymtCatalogsIWapper
						.selectProductIdBySku(skuId);
				Logger.verifyEquals(1, retList.size(), "SKU:" + skuId
						+ ", 应该只有一个对应的catalog");
				Map<String, Object> ret = retList.get(0);

				String productId = ret.get("sProductId").toString();
				String catalogId = ret.get("sCatalogId").toString();

				double actPriceInSql = Double.parseDouble(ret
						.get("fQuotePrice").toString());
				double actVipPriceInSql = Double.parseDouble(ret.get(
						"fVipPrice").toString());
				double actNewPriceInSql = Double.parseDouble(ret.get(
						"fNewCustomerPrice").toString());

				Logger.verifyEquals(tgtPrice, actPriceInSql, "Sql price检查 SKU:"
						+ skuId);
				Logger.verifyEquals(tgtVipPrice, actVipPriceInSql,
						"Sql vip price检查 SKU:" + skuId);
				Logger.verifyEquals(tgtNewPrice, actNewPriceInSql,
						"Sql new price检查 SKU:" + skuId);

				//mongo
				if (YmtMongoDriverFactroy.isNewMongoCheck()) {
					ProductDetailModelWapper productDetailModelWapper = new ProductDetailModelWapper();
					Map<String, Object> dataInMgo = productDetailModelWapper
							.getProductId(productId);
					JSONArray catalogAry = new JSONArray(dataInMgo.get(
							"CatalogList").toString());

					boolean isFound = false;
					for (int i = 0; i < catalogAry.length(); i++) {
						JSONObject catalogBean = catalogAry.getJSONObject(i);
						if (catalogBean.getString("CatalogId")
								.equals(catalogId)) {
							Logger.verifyEquals(skuId,
									catalogBean.getString("SKU"),
									"Mongo中sku检查 catalogId:" + catalogId
											+ ", sku:" + skuId);

							JSONArray priceList = catalogBean
									.getJSONArray("PriceList");
							boolean isAllPriceFound = true;
							for (int k = 0; k < priceList.length(); k++) {
								//price
								if (priceList.getJSONObject(k).getInt(
										"PriceType") == 0) {
									Logger.verifyEquals(tgtPrice, Double
											.parseDouble(priceList
													.getJSONObject(k)
													.getString("Price")),
											"Mongo price检查 SKU:" + skuId);
									continue;
								} else {
									isAllPriceFound = false;
								}
								//vip
								if (priceList.getJSONObject(k).getInt(
										"PriceType") == 2) {
									Logger.verifyEquals(tgtVipPrice, Double
											.parseDouble(priceList
													.getJSONObject(k)
													.getString("Price")),
											"Mongo vip price检查 SKU:" + skuId);
									continue;
								} else {
									isAllPriceFound = false;
								}

								//new
								if (priceList.getJSONObject(k).getInt(
										"PriceType") == 1) {
									Logger.verifyEquals(tgtNewPrice, Double
											.parseDouble(priceList
													.getJSONObject(k)
													.getString("Price")),
											"Mongo new price检查 SKU:" + skuId);
									continue;
								} else {
									isAllPriceFound = false;
								}

							}
							Logger.verifyEquals(true, isAllPriceFound,
									"scatalogId:" + catalogId + ", sku:"
											+ skuId + ", 有价格节点不存在mongo中");

							isFound = true;
							break;
						}
					}

					Logger.verifyEquals(true, isFound, "scatalogId:"
							+ catalogId + ", sku:" + skuId + ", 应该在mongo库中存在");

				}

				//java mongo
				if (YmtMongoDriverFactroy.isNewJavaMgoCheck()) {
					CatalogsWapper catalogsWapper = new CatalogsWapper();
					Map<String, Object> dataInMgo = catalogsWapper
							.getByCatalogId(catalogId);
					double actPriceInMgo = Double.parseDouble(dataInMgo.get(
							"price").toString());
					double actVipPriceInMgo = Double.parseDouble(dataInMgo.get(
							"vip").toString());
					double actNewPriceInMgo = Double.parseDouble(dataInMgo.get(
							"newp").toString());

					Logger.verifyEquals(tgtPrice, actPriceInMgo,
							"Java Mongo price检查 SKU:" + skuId);
					Logger.verifyEquals(tgtVipPrice, actVipPriceInMgo,
							"Java Mongo vip price检查 SKU:" + skuId);
					Logger.verifyEquals(tgtNewPrice, actNewPriceInMgo,
							"Java Mongo new price检查 SKU:" + skuId);

				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		});
	}
}